import 'package:flutter/material.dart';
import 'breakpoints.dart';

/// 企业级响应式布局组件
/// 根据屏幕尺寸自动选择对应的布局方案
class ResponsiveLayout extends StatelessWidget {
  final Widget mobile;
  final Widget? tablet;
  final Widget? desktop;
  final Widget? largeDesktop;
  
  const ResponsiveLayout({
    Key? key,
    required this.mobile,
    this.tablet,
    this.desktop,
    this.largeDesktop,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints) {
        final deviceType = AppBreakpoints.getDeviceType(context);
        
        switch (deviceType) {
          case DeviceType.largeDesktop:
            return largeDesktop ?? desktop ?? tablet ?? mobile;
          case DeviceType.desktop:
            return desktop ?? tablet ?? mobile;
          case DeviceType.tablet:
            return tablet ?? mobile;
          case DeviceType.mobile:
          default:
            return mobile;
        }
      },
    );
  }
}

/// 响应式导航栏组件
/// 根据屏幕尺寸自动切换底部导航栏和侧边导航栏
class ResponsiveNavigation extends StatelessWidget {
  final int currentIndex;
  final ValueChanged<int> onDestinationSelected;
  final List<NavigationDestination> destinations;
  
  const ResponsiveNavigation({
    Key? key,
    required this.currentIndex,
    required this.onDestinationSelected,
    required this.destinations,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ResponsiveLayout(
      mobile: _buildMobileNavigation(),
      tablet: _buildTabletNavigation(),
      desktop: _buildDesktopNavigation(),
      largeDesktop: _buildDesktopNavigation(),
    );
  }

  /// 手机端底部导航栏
  Widget _buildMobileNavigation() {
    return NavigationBar(
      selectedIndex: currentIndex,
      onDestinationSelected: onDestinationSelected,
      destinations: destinations,
    );
  }

  /// 平板端侧边导航栏
  Widget _buildTabletNavigation() {
    return NavigationRail(
      selectedIndex: currentIndex,
      onDestinationSelected: onDestinationSelected,
      destinations: destinations
          .map((dest) => NavigationRailDestination(
                icon: dest.icon,
                selectedIcon: dest.selectedIcon,
                label: Text(dest.label),
              ))
          .toList(),
      labelType: NavigationRailLabelType.all,
    );
  }

  /// 桌面端侧边导航栏（带扩展）
  Widget _buildDesktopNavigation() {
    return NavigationRail(
      selectedIndex: currentIndex,
      onDestinationSelected: onDestinationSelected,
      destinations: destinations
          .map((dest) => NavigationRailDestination(
                icon: dest.icon,
                selectedIcon: dest.selectedIcon,
                label: Text(dest.label),
              ))
          .toList(),
      extended: true,
      labelType: NavigationRailLabelType.none,
    );
  }
}

/// 响应式网格组件
class ResponsiveGrid extends StatelessWidget {
  final List<Widget> children;
  final double childAspectRatio;
  
  const ResponsiveGrid({
    Key? key,
    required this.children,
    this.childAspectRatio = 1.0,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ResponsiveLayout(
      mobile: _buildGrid(2), // 手机端2列
      tablet: _buildGrid(3), // 平板端3列
      desktop: _buildGrid(4), // 桌面端4列
      largeDesktop: _buildGrid(6), // 大屏6列
    );
  }

  Widget _buildGrid(int crossAxisCount) {
    return GridView.count(
      crossAxisCount: crossAxisCount,
      childAspectRatio: childAspectRatio,
      padding: EdgeInsets.all(16),
      mainAxisSpacing: 16,
      crossAxisSpacing: 16,
      children: children,
    );
  }
}